package com.lcq.demo.service;

import com.lcq.demo.common.converter.NewsDTOConverter;
import com.lcq.demo.common.converter.NewsPOConverter;
import com.lcq.demo.common.entity.PageResult;
import com.lcq.demo.entity.dto.NewsDTO;
import com.lcq.demo.entity.po.News;
import com.lcq.demo.repository.NewsRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 240701005
 */
@Service
public class NewsService {

    @Resource
    private NewsRepository newsRepository;

    public NewsDTO getById(Long id) {
        return NewsPOConverter.INSTANCE.convertPoToDto(newsRepository.getById(id));
    }

    public Boolean addNews(NewsDTO newsDTO) {
        newsDTO.setNewsStatus(0);
//        newsDTO.setNewsTime(new Date(System.currentTimeMillis()));
        newsRepository.save(NewsDTOConverter.INSTANCE.convertDtoToPo(newsDTO));
        return true;
    }

    public Boolean updateNews(NewsDTO newsDTO) {
        newsDTO.setNewsStatus(newsRepository.getById(newsDTO.getId()).getNewsStatus());
        newsRepository.save(NewsDTOConverter.INSTANCE.convertDtoToPo(newsDTO));
        return true;
    }

    public Boolean listOrDelistNews(NewsDTO newsDTO) {
        News news = newsRepository.getById(newsDTO.getId());
        if (news.getNewsStatus() == 0) {
            news.setNewsStatus(1);
        } else {
            news.setNewsStatus(0);
        }
        newsRepository.save(news);
        return true;
    }

    public Boolean deleteNews(Long id) {
        try {
            newsRepository.deleteById(id);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public PageResult<NewsDTO> getNewsList(NewsDTO newsDTO) {
        PageResult<NewsDTO> pageResult = new PageResult<>();
        Specification<News> specification = (Root<News> root, CriteriaQuery<?> query, CriteriaBuilder cb) -> {
            List<Predicate> predicates = new ArrayList<>();
            if (newsDTO.getNewsStatus() != null) {
                predicates.add(cb.equal(root.get("newsStatus"), newsDTO.getNewsStatus()));
            }
            if (newsDTO.getStartTime() != null && newsDTO.getEndTime() != null) {
                predicates.add(cb.between(root.get("newsTime"), newsDTO.getStartTime(), newsDTO.getEndTime()));
            }
            return cb.and(predicates.toArray(new Predicate[0]));
        };
        long count = newsRepository.count(specification);
        Pageable pageable = PageRequest.of(newsDTO.getPageNum() - 1, newsDTO.getPageSize());
        Page<News> pageRes = newsRepository.findAll(specification, pageable);
        List<NewsDTO> newsDTOList = pageRes.getContent().stream()
                            .map(NewsPOConverter.INSTANCE::convertPoToDto)
                            .collect(Collectors.toList());
        pageResult.setResult(newsDTOList);
        pageResult.setPageSize(newsDTO.getPageSize());
        pageResult.setPageNo(newsDTO.getPageNum());
        pageResult.setTotal((int) count);
        return pageResult;
    }
}
